//
//  FGTabBarItem.swift
//  FGApp
//
//  Created by ryan on 2019/4/28.
//  Copyright © 2019 ryan. All rights reserved.
//

import UIKit

class FGTabBarItem: UIControl {

    var itemDic : Dictionary<String ,String>
    
    var imgView : UIImageView
    
    var titleLabel : UILabel
    
    var lineV : UIView
    
    
    /// 属性观察器
    var currentSelectState = false {
        didSet{
            // select
            if currentSelectState {
                imgView.image = UIImage (named: itemDic["select_img"]!)
                titleLabel.textColor = UC_Subject
            }else{
                // no select
                imgView.image = UIImage(named: itemDic["normal_img"]!)
                titleLabel.textColor = UC_LightGray
            }
        }
    }
    
    init(frame: CGRect, itemDic:Dictionary<String, String>, itemIndex: Int) {
        self.itemDic = itemDic
        
        let defaultLabelH : CGFloat = 20.0
        
        let imgTop : CGFloat = 8
        
        let imgWidth : CGFloat = 19
        
        // 中间按钮特殊处理
        /*
        if itemIndex == 2 {
            imgTop = -20
            imgWidth = 50
        }
        */
        let imgLeft : CGFloat = (frame.size.width - imgWidth)/2
        
//        let imgHeight : CGFloat = frame.size.height - defaultLabelH - imgTop
        
        // 图片
        imgView = UIImageView (frame: CGRect (x: imgLeft, y: imgTop, width: imgWidth, height: imgWidth))
        imgView.image = UIImage (named: itemDic["normal_img"]!)
        imgView.contentMode = .scaleAspectFit
        
        // title
        titleLabel = UILabel (frame: CGRect (x: 0, y: frame.height-defaultLabelH, width: frame.size.width, height: defaultLabelH))
        titleLabel.text = itemDic["titleName"]!
        titleLabel.textAlignment = .center
        titleLabel.font = UIFont (name: PingFang_Regular, size: 10)
        titleLabel.textColor = UC_LightGray
        
        lineV = UIView(frame: CGRect (x: 0, y: 0, width: KScreenWidth, height: 1))
        lineV.backgroundColor = UIColor.hex(hexString: "#EDEDED")
        
        super.init(frame: frame)
        self.addSubview(imgView)
        self.addSubview(titleLabel)
        self.addSubview(lineV)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
